package edu.tjhsst.androidIntranet.period8;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import edu.tjhsst.androidIntranet.IntranetManager;
import edu.tjhsst.androidIntranet.PageLoader;

import android.os.Handler;
import android.util.Log;

public class EighthLoaderThread extends Thread {

	private static final String TAG = "EighthLoaderThread";
	private boolean loadNextWeek;
	private Handler h;
	private EighthBlockPicker ebp;
	private static GregorianCalendar startDate;

	public EighthLoaderThread(Handler eighthHandler,
			EighthBlockPicker eighthBlockPicker, boolean loadNextTwoWeeks) {
		this.h=eighthHandler;
		this.ebp=eighthBlockPicker;
		if(loadNextTwoWeeks) {
			if(startDate!=null) {
				startDate.add(Calendar.DAY_OF_YEAR, 14);
			} else {
				GregorianCalendar now=new GregorianCalendar();
				startDate=new GregorianCalendar(now.get(Calendar.YEAR),now.get(Calendar.MONTH),now.get(Calendar.DAY_OF_MONTH));
			}
		} else {
			GregorianCalendar now=new GregorianCalendar();
			startDate=new GregorianCalendar(now.get(Calendar.YEAR),now.get(Calendar.MONTH),now.get(Calendar.DAY_OF_MONTH));
		}
	}

	public void run() {
		if(!loadNextWeek) {
			//load stuff here

			//loadMainPage();
			//because we're not caching stuff yet, this isn't useful

			loadFullSchedulePage();
		} else {
		}
	}

	private void loadFullSchedulePage() {
		Log.d(TAG, "Downloading: " + IntranetManager.SERVER + "eighth/vcp_schedule/view/uid/" + IntranetManager.getUID() + "/start_date/"+startDate.get(Calendar.YEAR)+"-"+String.format("%02d", (startDate.get(Calendar.MONTH)+1))+"-"+String.format("%02d", startDate.get(Calendar.DAY_OF_MONTH)));
		PageLoader.downloadPage("eighth/vcp_schedule/view/uid/" + IntranetManager.getUID() + "/start_date/"+startDate.get(Calendar.YEAR)+"-"+String.format("%02d", (startDate.get(Calendar.MONTH)+1))+"-"+String.format("%02d", startDate.get(Calendar.DAY_OF_MONTH)));
		//IntranetManager.showErrorMessage(ebp, PageLoader.getPage());
		
		StringBuffer page = PageLoader.getPage();
		
		//TODO: needs capture groups and wildcards
		Pattern eighthPattern = Pattern.compile("<tr class=\"c\\d\">"
			//+".+?"+"<td style=\"text-align: center;\"><input type=\"checkbox\" name=\"bids\\[\\]\" value=\"(\\d+?)\" onclick=\"CCA\\(this\\);\" /></td>."
			//+"<td style=\"text-align: center;\"><a href=\"https://iodine.tjhsst.edu/eighth/vcp_schedule/choose/uid/12799/bids/1902/start_date/2010-05-02\">Change</a></td>."
			//+"<td style=\"text-align: center;\">Tue</td>."
			//+"<td style=\"text-align: center;\">May  4, 2010</td>." 
			//+"<td style=\"text-align: center;\">A</td>."
			//+"<td style=\"text-align: center;\">---</td>."
			//+"<td style=\"text-align: center;\">HAS NOT SELECTED AN ACTIVITY</td>."
			//+"<td style=\"text-align: center;\"></td>."
			//+"<td style=\"text-align: center;\">236WK</td>."
			//+"</tr> "
			, Pattern.DOTALL);
		Matcher m = eighthPattern.matcher(page);
		Log.d(TAG, "Searching...");
		while(m.find()) {
			Log.d(TAG, "Found: " + m.group());
		}
		
	}

}
